package cn.wangzg.course0801;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import cn.wangzg.course0801.adapter.CommonAdapter;
import cn.wangzg.course0801.adapter.ViewHolder;
import cn.wangzg.course0801.bean.Article;
import cn.wangzg.course0801.util.HttpCallbackModelListener;
import cn.wangzg.course0801.util.HttpCallbackStringListener;
import cn.wangzg.course0801.util.HttpUrlConnectionUtils;

public class MainActivity extends AppCompatActivity {
    private ListView listArticle;
    private CommonAdapter<Article.DataBean.DatasBean> adapter;
    //分页加载使用
    private int mTotalItemCount;//item的总数
    private int mLastVisibleItemIndex;//当前可见的最后一项
    private boolean isLoading=false;//是否正在加载，避免重复加载
    private int curPage=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        getData();
    }
    //抽取方法:Ctrl+Alt+M(ethod)
    private void initView() {
        listArticle=findViewById(R.id.listArticle);
        adapter=new CommonAdapter<Article.DataBean.DatasBean>(this,null,
                R.layout.item_article) {
            @Override
            public void convert(ViewHolder helper, final Article.DataBean.DatasBean item) {
                helper.setText(R.id.textView, item.getTitle());
                helper.setText(R.id.textView3, item.getShareUser());
                helper.setText(R.id.textView5, item.getSuperChapterName()
                        + "/" + item.getChapterName());
                helper.setText(R.id.textView7, item.getNiceDate());
                helper.getView(R.id.imageView).setSelected(item.isCollect());
                helper.getView(R.id.imageView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        v.setSelected(!v.isSelected());
                        item.setCollect(v.isSelected());
                    }
                });
            }
        };
        listArticle.setAdapter(adapter);
        /**
         * 如何判断到达底部？
         * 1.判断？
         * 2.加载下一页
         */
        listArticle.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //scrollState有三种状态，分别是开始滚动（SCROLL_STATE_FLING），
                // 正在滚动(SCROLL_STATE_TOUCH_SCROLL), 已经停止（SCROLL_STATE_IDLE）
                if (mTotalItemCount == mLastVisibleItemIndex && scrollState == SCROLL_STATE_IDLE) {
                    if(!isLoading){
                        isLoading=true;
                        HttpUrlConnectionUtils.doGet(MainActivity.this,
                                "https://www.wanandroid.com/article/list/"+curPage+"/json",
                                new HttpCallbackModelListener() {
                                    @Override
                                    public void onFinish(Object response) {
                                        Article article= (Article) response;
                                        adapter.addmDatas(article.getData().getDatas());

                                        curPage++;
                                        isLoading=false;
                                    }

                                    @Override
                                    public void onError(Exception e) {

                                    }
                                },Article.class);
                    }
                }
            }

            /**
             * firstVisibleItem+visibleItemCount==totalItemCount
             * 成立，是不是已经到达底部了？
             * @param view              视图
             * @param firstVisibleItem  可见的第一项的索引（id）
             * @param visibleItemCount  可见的item的数量
             * @param totalItemCount    Item的总数
             */
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                mLastVisibleItemIndex=firstVisibleItem+visibleItemCount;
                mTotalItemCount=totalItemCount;
            }
        });
    }

    private void getData() {
//        HttpUrlConnectionUtils.doGet(this,
//                "https://www.wanandroid.com/article/list/0/json",
//                new HttpCallbackStringListener() {
//                    @Override
//                    public void onFinish(String response) {
//                        //tv_html.setText(response);
//                        Gson gson=new Gson();
//                        Article article = gson.fromJson(response, Article.class);
//                        article.getData().getDatas();
//                    }
//
//                    @Override
//                    public void onError(Exception e) {
//
//                    }
//                });
        HttpUrlConnectionUtils.doGet(this,
                "https://www.wanandroid.com/article/list/0/json",
                new HttpCallbackModelListener() {
                    @Override
                    public void onFinish(Object response) {
                        Article article= (Article) response;
                        adapter.setmDatas(article.getData().getDatas());
                        curPage=1;
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                },Article.class);
    }
}
